@echo off
setlocal

set MODE=R
set ARCH=32
set SUBSYSTEM=console
set SUBSYSTEM_VERSION=,5.01
set DEFINES=

call :parse_args %*

call D:\vcvars%ARCH%.bat

del *.exe *.pdb

echo MZ............................................................> t

set CFLAGS=/nologo /GS- /Gy- /GR- /Oi- /W4 /WX /external:W0 /external:anglebrackets %DEFINES%
set LDFLAGS=/nologo /nodefaultlib /incremental:no /safeseh:no /merge:.rdata=.text /EMITPOGOPHASEINFO /subsystem:%SUBSYSTEM%%SUBSYSTEM_VERSION% /stub:t /ALIGN:16
set LIBS=kernel32.lib wininet.lib shell32.lib

if %MODE% == D (
    set CFLAGS=%CFLAGS% /Zi /MTd
    set LDFLAGS=%LDFLAGS% /debug
) else (
    set CFLAGS=%CFLAGS% /O1 /MT
    set LDFLAGS=%LDFLAGS% /release
)

for %%i in (*.c,*.cpp) do (
    cl %CFLAGS% %%i /link %LDFLAGS% %LIBS% || goto err
)

del *.obj t

exit /b

:err

del *.obj t

exit /b 1

:parse_args
if x%1 == x exit /b
if x%1 == xD set MODE=D
if x%1 == x64 (
    set ARCH=64
    set SUBSYSTEM_VERSION=
)
if x%1 == xW set SUBSYSTEM=windows
if x%1 == xU set DEFINES=%DEFINES% /D_UNICODE /DUNICODE
shift /1
goto parse_args